package net.qhzw.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.qhzw.admin.entity.SysPermission;
import net.qhzw.admin.entity.SysRole;
import net.qhzw.admin.entity.SysRolePermission;
import net.qhzw.admin.mapper.SysPermissionMapper;
import net.qhzw.admin.mapper.SysRolePermissionMapper;
import net.qhzw.admin.mapper.SysRoleMapper;
import net.qhzw.admin.param.RoleParam;
import net.qhzw.admin.pojo.SysRoleVo;
import net.qhzw.admin.query.RoleQuery;
import net.qhzw.admin.service.SysRoleService;
import net.qhzw.common.model.JsonResult;
import net.qhzw.common.util.StringUtils;
import net.qhzw.common.util.UUIDUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysPermissionMapper permissionMapper;

    @Autowired
    private SysRolePermissionMapper rolePermissionMapper;

    @Override
    public JsonResult getRole(String id) {
        SysRole sysRole = roleMapper.selectByPrimaryKey(id);
        SysRoleVo sysRoleVo = new SysRoleVo();
        BeanUtils.copyProperties(sysRole, sysRoleVo);
        if (sysRole != null && sysRole.getId() != null) {
            List<SysPermission> permissions = permissionMapper.selectByRoleKey(id);
            sysRoleVo.setPermissions(permissions);
        }
        return JsonResult.success(sysRoleVo);
    }


//    @Override
//    public JsonResult getRole(String id) {
//        RoleQuery query = new RoleQuery();
//        query.setId(id);
//        List<SysRoleVo> sysRoleVos = roleMapper.selectRoleVoList(query);
//        if (sysRoleVos == null || sysRoleVos.isEmpty()) {
//            return JsonResult.success();
//        }
//        SysRoleVo sysRoleVo = sysRoleVos.get(0);
//        return JsonResult.success(sysRoleVo);
//    }

    @Override
    public JsonResult getRoleAll(RoleQuery query) {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<SysRole> roles = roleMapper.selectList(query);
        List<SysRoleVo> roleVos = new ArrayList<>();
        for (SysRole role : roles) {
            SysRoleVo sysRoleVo = new SysRoleVo();
            BeanUtils.copyProperties(role, sysRoleVo);
            if (sysRoleVo != null && sysRoleVo.getId() != null) {
                List<SysPermission> permissions = permissionMapper.selectByRoleKey(role.getId());
                sysRoleVo.setPermissions(permissions);
                roleVos.add(sysRoleVo);
            }
        }
        PageInfo<SysRoleVo> pageInfo = new PageInfo<>(roleVos);
        resultMap.put("total", pageInfo.getTotal());
        resultMap.put("list", pageInfo.getList());
        return JsonResult.success(resultMap);
    }

//    @Override
//    public JsonResult getRoleAll(RoleQuery query) {
//        Map<String, Object> resultMap = new HashMap<>();
//        PageHelper.startPage(query.getPage(), query.getLimit());
//        List<SysRoleVo> sysRoleVos = roleMapper.selectRoleVoList(query);
//        PageInfo<SysRoleVo> pageInfo = new PageInfo<>(sysRoleVos);
//        Integer total = roleMapper.selectRoleVoTotal(query);
//        resultMap.put("total", total);
//        resultMap.put("list", pageInfo.getList());
//        return JsonResult.success(resultMap);
//    }

    @Transactional
    @Override
    public JsonResult edit(RoleParam role) {
        if (role.getId() == null || StringUtils.isEmpty(role.getId())) {
            //添加
            SysRole sysRole = new SysRole();
            BeanUtils.copyProperties(role, sysRole);
            String uUid = UUIDUtil.getUUid();
            sysRole.setId(uUid);
            sysRole.setCreateTime(new Date());
            sysRole.setStatus("1");
            int row = roleMapper.insertSelective(sysRole);
            List<String> sysMenus = role.getSysMenus();
            for (String sysMenu : sysMenus) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                sysRolePermission.setId(UUIDUtil.getUUid());
                sysRolePermission.setRoleId(uUid);
                sysRolePermission.setPermissionId(sysMenu);
                rolePermissionMapper.insertSelective(sysRolePermission);
            }
            return JsonResult.result(row, "新增失败");
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(role, sysRole);
        int count = rolePermissionMapper.deleteByRoleId(sysRole.getId());
        if (count < 0) {
            return JsonResult.error("修改失败");
        }
        int row = roleMapper.updateByPrimaryKeySelective(sysRole);


        List<String> sysMenus = role.getSysMenus();
        for (String sysMenu : sysMenus) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(UUIDUtil.getUUid());
            sysRolePermission.setRoleId(role.getId());
            sysRolePermission.setPermissionId(sysMenu);
            rolePermissionMapper.insertSelective(sysRolePermission);
        }
        return JsonResult.result(row, "修改失败");
    }

    @Override
    public JsonResult deleteRole(String id) {
        int row = roleMapper.deleteByPrimaryKey(id);
        rolePermissionMapper.deleteByRoleId(id);
        return JsonResult.result(row, "删除失败");
    }
}
